import Event from '../Events'


/**
 * 基于 document 的鼠标移动层监听事件对象
 *
 * @class MoveMark
 * @extends {Event}
 */
class MoveMark extends Event {
    constructor() {
        super();
        this.shade = null;
        this.positionCatch = [];
        this.positionNow = [];
        this.status = false;
        this._MoveEvent = (event) => {
            this.MoveEvent(event);
        }
        this._MoveEventEnd = (event) => {
            this.MoveEventEnd(event)
        }
    }
    MoveBoardStart() {
        this.shade = document.createElement('div');
        this.shade.style.position = 'fixed';
        this.shade.style.top = '0';
        this.shade.style.left = '0';
        this.shade.style.right = '0';
        this.shade.style.bottom = '0';
        this.shade.style.zIndex = '999999';
        this.shade.style.cursor = 'move';
        this.shade.onmousedown = (event) => {
            this.status = true;
            this.positionCatch = [event.clientX, event.clientY];
            document.addEventListener('mousemove', this._MoveEvent);
            document.addEventListener('mouseup', this._MoveEventEnd);
        }
        document.body.appendChild(this.shade);
    }
    clear() {
        if (this.status) {
            document.removeEventListener('mousemove', this._MoveEvent);
            document.removeEventListener('mouseup', this._MoveEventEnd);
            this.status = false;
        }
    }
    MoveBoardEnd() {
        if (this.status) {
            this.MoveBoard(true);
        }
        this.shade.remove();
        this.shade.onmousedown = null;
        this.shade = null;
        this.clear();
    }
    MoveEvent(event) {
        this.positionNow = [event.clientX, event.clientY];
        this.MoveBoard(false);
    }
    MoveEventEnd(event) {
        this.positionNow = [event.clientX, event.clientY];
        this.MoveBoard(true);
        this.clear();
    }
    MoveBoard(status) {
        var dx = this.positionNow[0] - this.positionCatch[0];
        var dy = this.positionNow[1] - this.positionCatch[1];
        this.run('Move', dx, dy, status);
    }
}


export default MoveMark;